<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Goods extends Model
{
    protected $table = "goods";
    protected $appends = ["banner","image_text","detail_text","express_type"];

    public function getBannerAttribute()
    {
        $exp = explode(',',$this->attributes['image']);
        $banner = [];
        foreach ($exp as $item){
            $banner[] = Wechart::PATH_IMAGE.'uploads/'.$item;
        }
        return $banner;
    }

    public function getImageAttribute(){
        $exp = explode(',',$this->attributes['image']);
        return Wechart::PATH_IMAGE.'uploads/'.$exp[0];
    }


    public function getDetailAttribute()
    {
        $exp = explode(',',$this->attributes['detail']);
        $detail = [];
        foreach ($exp as $item){
            $detail[] = Wechart::PATH_IMAGE.'uploads/'.$item;
        }
        return $detail;
    }


    public function getDetailTextAttribute()
    {
       return $this->attributes['detail'];
    }


    public function getImageTextAttribute()
    {
        return $this->attributes['image'];
    }

    public function getIsFreeShoppingAttribute()
    {
        if ($this->attributes['is_free_shopping']==1){
            return '包邮';
        }else{
            return '到付';
        }
    }
    public function getExpressTypeAttribute()
    {
        return $this->attributes['is_free_shopping'];
    }

    public function sku(){
        return $this->hasMany(GoodsSku::class,"goods_id","id");
    }

    public function color(){
        return $this->hasMany(GoodsColor::class,"goods_id","id");
    }
}
